<template>
  <div id="box">
    <el-card class="box-card">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane id="" label="用户管理" name="first">
          <el-row>
            <el-button
              type="primary"
              @click.stop="handleEdit"
            >+新增角色</el-button>
          </el-row>
          <el-row>
            <el-table
              ref="singleTable"
              :data="tableData"
              highlight-current-row
              style="width: 100%"
            >
              <el-table-column label="序号" type="index" width="80" />
              <el-table-column property="name" label="角色名称" width="150" />
              <el-table-column property="description" label="描述" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="success"
                    @click.stop="handleEdit(scope.$index, scope.row)"
                  >分配权限</el-button>
                  <el-button
                    size="mini"
                    type="primary"
                    @click.stop="handleEdit(scope.$index, scope.row)"
                  >编辑</el-button>
                  <el-button
                    size="mini"
                    type="danger"
                    @click.stop="handleDelete(scope.$index, scope.row)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <el-pagination
        :current-page="params.page"
        :page-sizes="[1, 3, 4, 5]"
        :page-size="params.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <!-- <setting-card :show-card="showCard" @cansle="showCard=false" /> -->
    <el-dialog :title="formTitle" :visible.sync="dialogFormVisible">
      <el-form ref="infoForm" v-model="form" :model="form" :rules="rules">
        <el-form-item
          label="角色名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="角色描述"
          :label-width="formLabelWidth"
          prop="description"
        >
          <el-input v-model="form.description" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cansel">取 消</el-button>
        <el-button type="primary" @click="enter('true')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getUserList, delUser, editUser, addUser } from '@/api/setting'
// import SettingCard from './settingCard.vue'
export default {
  components: {
    // SettingCard
  },
  data() {
    return {
      activeName: 'first',
      tableData: [],
      total: 0,
      params: {
        page: 1,
        pagesize: 3
      },
      dialogFormVisible: false,
      rules: {
        name: [{ required: true, message: '此项为必填项', trigger: 'blur' }],
        description: [{ required: true, message: '此项为必填项', trigger: 'blur' }]
      },
      form: {
        name: '',
        description: ''
      },
      formTitle: '添加角色',
      formLabelWidth: '120px'
    }
  },
  created() {
    this.getList()
  },
  mounted() {},
  methods: {
    handleClick(tab, event) {
      console.log(tab, event)
    },
    // resetform重置表单事件
    resetForm() {
      console.log('重置表单')
      this.form = {
        name: '',
        description: ''
      }
    },
    // 编辑
    handleEdit(index, row) {
      if (row) {
        this.formTitle = '编辑角色'
        const { name, description, id } = row
        this.form.name = name
        this.form.description = description
        this.form.id = id
        // this.resetForm()
      } else {
        this.formTitle = '添加角色'
      }
      this.dialogFormVisible = true
      console.log(index, row)
    },
    // 确认
    enter() {
      this.$refs.infoForm.validate(async val => {
        if (val) {
          console.log('提交')
          if (this.formTitle === '编辑角色') {
            const res = await editUser(this.form.id, this.form)
            this.$refs.infoForm.resetFields()
            if (res.status === 200) {
              this.$message.success('更改信息成功')
              this.getList()
            }
          } else if (this.formTitle === '添加角色') {
            const res = await addUser(this.form)
            console.log(res)
            this.$message.success('添加角色成功')
            this.getList()
          }
          this.resetForm()
        }
      })
      this.dialogFormVisible = false
    },
    // 取消
    cansel() {
      console.log('refs', this.$refs)
      this.dialogFormVisible = false
      // this.$refs.infoForm.resetFields()
      this.resetForm()
    },
    // 删除
    handleDelete(index, row) {
      console.log(index, row)
      this.$confirm('确定不留一线生机?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delUser(row.id)
        console.log(res)
        this.getList()
      }
      )
    },
    // 获取数据
    async getList() {
      const { data } = await getUserList(this.params)
      console.log('全部数据', data)
      this.tableData = data.data.rows
      this.total = data.data.total
    },
    handleSizeChange(val) {
      this.params.pagesize = val
      this.getList()
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.params.page = val
      this.getList()
    }
  }
}
</script>

<style scoped>
#box {
  /* padding-left: 5vh !important; */
}
</style>
